import 'package:appflutterhomeschool/res/styles.dart';
import 'package:appflutterhomeschool/utils/utils.dart';

///用户：Nine Tailed Fox
///时间：2020/6/8
///邮箱：1037438704@qq.com
///功能：AppBar

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class MyAppBar extends StatefulWidget implements PreferredSizeWidget {
  /// 返回的动作，默认是返回，如果实现了要在方法里pop
  final VoidCallback backAction;

  /// 背景颜色，默认为 Colors.white
  final Color backgroundColor;

  /// 标题
  final String title;

  /// 字体颜色和返回按钮icon的颜色，默认为ColorHelper.gBlackColor - #191919
  final Color color;

  /// 标题字体的权重，默认为 FontWeight.w500
  final FontWeight fontWeight;

  /// 状态栏字体的白色或黑色 Brightness.light-字体黑色，Brightness.dark-字体白色
  /// 默认为 Brightness.light
  final Brightness brightness;

  /// 下横线
  final double elevation;
  final VoidCallback onTapLeft;
  final VoidCallback onTapRight;
  final String imageRightUrl;
  final bool rightIconVisible;
  final bool finishBool;

  MyAppBar(
      {this.title,
      this.elevation,
      this.onTapLeft,
      this.imageRightUrl,
      this.onTapRight,
      this.finishBool,
      this.backAction,
      this.backgroundColor,
      this.rightIconVisible,
      this.color,
      this.fontWeight,
      this.brightness});

  @override
  _MyAppBarState createState() => _MyAppBarState();

  @override
  Size get preferredSize => Size.fromHeight(56.0);
}

class _MyAppBarState extends State<MyAppBar> {
  @override
  Widget build(BuildContext context) {
    return AppBar(
      title: Text(widget.title ?? "",
          style: TextStyle(
              color: Colors.black, fontSize: ScreenUtil().setWidth(43))),
      elevation: widget.elevation ?? 4.0,
      leading: IconButton(
        icon:widget.finishBool ? Image.asset(Utils.getImgPath("caidan")):Icon(
          Icons.arrow_back_ios,
          size: ScreenUtil().setSp(40),
          color: Colors.black,
        ),
        onPressed: widget.onTapLeft,
      ),
      brightness:
          widget.brightness == null ? Brightness.light : widget.brightness,
      actions: <Widget>[
        Visibility(
          visible: widget.rightIconVisible??true,
          child: GestureDetector(
            child: Image.asset(Utils.getImgPath(widget.imageRightUrl??"shaixuan_false")),
            onTap: widget.onTapRight,
          ),
        )
      ],

      /// 为了适配安卓标题居中
      centerTitle: true,
      backgroundColor: widget.backgroundColor == null
          ? Colors.white
          : widget.backgroundColor,
      iconTheme: IconThemeData(),
    );
  }
}

class MyAppBar2 extends StatefulWidget implements PreferredSizeWidget {
  /// 返回的动作，默认是返回，如果实现了要在方法里pop
  final VoidCallback backAction;

  /// 背景颜色，默认为 Colors.white
  final Color backgroundColor;

  /// 标题
  final String title;

  /// 字体颜色和返回按钮icon的颜色，默认为ColorHelper.gBlackColor - #191919
  final Color color;

  /// 标题字体的权重，默认为 FontWeight.w500
  final FontWeight fontWeight;

  /// 状态栏字体的白色或黑色 Brightness.light-字体黑色，Brightness.dark-字体白色
  /// 默认为 Brightness.light
  final Brightness brightness;

  /// 下横线
  final double elevation;
  final VoidCallback onTapLeft;
  final Widget rightIcon;

  MyAppBar2(
      {this.title,
      this.elevation,
      this.onTapLeft,
      this.rightIcon,
      this.backAction,
      this.backgroundColor,
      this.color,
      this.fontWeight,
      this.brightness});

  @override
  _MyAppBar2State createState() => _MyAppBar2State();

  @override
  Size get preferredSize => Size.fromHeight(56.0);
}

class _MyAppBar2State extends State<MyAppBar2> {
  @override
  Widget build(BuildContext context) {
    return AppBar(
      title: Text(widget.title ?? "", style: TextStyles.textDarkBold43),
      elevation: widget.elevation ?? 4.0,
      leading: new IconButton(
        icon: Icon(
          Icons.arrow_back_ios,
          size: ScreenUtil().setSp(40),
          color: Colors.black,
        ),
        onPressed: () {
          if(widget.onTapLeft != null){
            widget.onTapLeft();
          }else{
            Navigator.pop(context);
          }
        },
      ),
    actions: <Widget>[
        widget.rightIcon??Container(),
      ],
      brightness:
          widget.brightness == null ? Brightness.light : widget.brightness,
      /// 为了适配安卓标题居中
      centerTitle: true,
      backgroundColor: widget.backgroundColor == null
          ? Colors.white
          : widget.backgroundColor,
      iconTheme: IconThemeData(),
    );
  }
}
